package com.xinqi.modules.prepare.question.controller.web;

import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.modules.prepare.question.dto.req.PrepareFolderQuestionSortDTO;
import com.xinqi.modules.prepare.question.dto.req.PrepareJoinQuestionFolderDTO;
import com.xinqi.modules.prepare.question.dto.req.PrepareQuestionFolderCreateDTO;
import com.xinqi.modules.prepare.question.dto.req.PrepareQuestionFolderIsExistNameDTO;
import com.xinqi.modules.prepare.question.dto.req.PrepareQuestionFolderQueryDTO;
import com.xinqi.modules.prepare.question.dto.req.PrepareQuestionFolderRefQueryDTO;
import com.xinqi.modules.prepare.question.dto.req.PrepareQuestionFolderUpdateDTO;
import com.xinqi.modules.prepare.question.dto.rsp.PrepareQuestionBankResultDTO;
import com.xinqi.modules.prepare.question.dto.rsp.PrepareQuestionFolderResultDTO;
import com.xinqi.modules.prepare.question.dto.rsp.PrepareQuestionStatsDataDTO;
import com.xinqi.modules.prepare.question.service.PrepareQuestionFolderRefService;
import com.xinqi.modules.prepare.question.service.PrepareQuestionFolderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 备课区试题夹APP控制层
 *
 * @author: system
 * @date: 2023/03/19
 */
@Tag(name = "Web-备课区-试题库", description = "备课区-试题库Web接口")
@RestController
@RequestMapping("/web/prepare")
@RequiredArgsConstructor
public class PrepareQuestionFolderWebController {
    private final PrepareQuestionFolderService prepareQuestionFolderService;
    private final PrepareQuestionFolderRefService prepareQuestionFolderRefService;

    /**
     * 备课区试题夹-分页
     */
    @Operation(summary = "备课区试题夹-分页")
    @GetMapping("/{prepare-id}/question-folder/page")
    public R<PageInfo<PrepareQuestionFolderResultDTO>> page(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                                            @ParameterObject PrepareQuestionFolderQueryDTO dto) {
        PageInfo<PrepareQuestionFolderResultDTO> page = prepareQuestionFolderService.page(dto, dto);
        return R.data(page);
    }

    /**
     * 备课区试题夹-详情
     */
    @Operation(summary = "备课区试题夹-详情")
    @GetMapping("/{prepare-id}/question-folder/{id}")
    public R<PrepareQuestionFolderResultDTO> findById(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                                      @Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        PrepareQuestionFolderResultDTO dto = prepareQuestionFolderService.findById(id);
        return R.data(dto);
    }

    /**
     * 新增备课区试题夹
     */
    @Operation(summary = "新增备课区试题夹")
    @PostMapping("/{prepare-id}/question-folder")
    public R<Long> insert(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                          @RequestBody PrepareQuestionFolderCreateDTO dto) {
        dto.setPrepareId(prepareId);
        return R.data(prepareQuestionFolderService.insert(dto));
    }

    /**
     * 修改备课区试题夹
     */
    @Operation(summary = "修改备课区试题夹")
    @PutMapping("/{prepare-id}/question-folder/{id}")
    public R<Boolean> update(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                             @Parameter(description = "id", example = "100001") @PathVariable("id") Long id,
                             @RequestBody PrepareQuestionFolderUpdateDTO dto) {
        dto.setId(id);
        return R.data(prepareQuestionFolderService.updateById(dto));
    }

    /**
     * 删除备课区试题夹
     */
    @Operation(summary = "删除备课区试题夹")
    @DeleteMapping("/{prepare-id}/question-folder")
    public R<Boolean> delete(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                             @Parameter(description = "课题夹列表", example = "[1, 2, 3]") @RequestBody List<Long> ids) {
        return R.data(prepareQuestionFolderService.delete(prepareId, ids));
    }

    /**
     * 判断试题夹名称是否存在
     */
    @Operation(summary = "判断试题夹名称是否存在")
    @GetMapping("/{prepare-id}/is-exist-name")
    public R<Boolean> isExistName(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                  @ParameterObject PrepareQuestionFolderIsExistNameDTO dto) {
        dto.setPrepareId(prepareId);
        return R.data(prepareQuestionFolderService.isExistName(dto));
    }

    /**
     * 查询试题夹中的试题（不返回总数）
     */
    @Operation(summary = "查询试题夹中的试题（不返回总数）")
    @GetMapping("/{prepare-id}/question-folder/{question-folder-id}/question/page")
    public R<PageInfo<PrepareQuestionBankResultDTO>> questionPage(@Parameter(description = "备课区ID", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                                                  @Parameter(description = "试题夹ID", example = "100001") @PathVariable("question-folder-id") Long questionFolderId,
                                                                  @ParameterObject PrepareQuestionFolderRefQueryDTO dto) {
        dto.setPrepareId(prepareId);
        dto.setQuestionFolderId(questionFolderId);
        return R.data(prepareQuestionFolderRefService.questionPage(dto));
    }

    /**
     * 查询试题夹中的试题的汇总信息
     */
    @Operation(summary = "查询试题夹中的试题的汇总信息")
    @GetMapping("/{prepare-id}/question-folder/{question-folder-id}/question/stats")
    public R<PrepareQuestionStatsDataDTO> questionStats(@Parameter(description = "备课区ID", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                                        @Parameter(description = "试题夹ID", example = "100001") @PathVariable("question-folder-id") Long questionFolderId) {
        return R.data(prepareQuestionFolderRefService.questionStats(prepareId, questionFolderId));
    }

    /**
     * 添加题目到试题夹
     */
    @Operation(summary = "添加题目到试题夹")
    @PutMapping("/{prepare-id}/question-folder/question/join")
    public R<Boolean> joinQuestionFolder(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                         @RequestBody PrepareJoinQuestionFolderDTO dto) {
        dto.setPrepareId(prepareId);
        return R.data(prepareQuestionFolderService.joinQuestionFolder(dto));
    }

    /**
     * 移除试题夹中的题目
     */
    @Operation(summary = "移除试题夹中的题目")
    @PutMapping("/{prepare-id}/question-folder/question/remove")
    public R<Boolean> removeQuestionFolder(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                           @RequestBody PrepareJoinQuestionFolderDTO dto) {
        dto.setPrepareId(prepareId);
        return R.data(prepareQuestionFolderService.removeQuestionFolder(dto));
    }

    /**
     * 课题夹中题目排序
     */
    @Operation(summary = "课题夹中题目排序")
    @PutMapping("/{prepare-id}/question-folder/question/sort")
    public R<Boolean> sortFolderQuestion(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                         @RequestBody PrepareFolderQuestionSortDTO dto) {
        dto.setPrepareId(prepareId);
        return R.data(prepareQuestionFolderService.sortFolderQuestion(dto));
    }


}
